home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-04 | 1.2 KB | 58 lines | [TEXT/MPS ] |
- Perl -Sx "{0}" {"Parameters"}; Exit
-
- #!perl
- # cpif [ -eq -ne ] file...
- # copy standard input to each of the named files
- # if new * old is true or old doesn't exist;
- # * defaults to -ne
-
- # set -x
- $op = "ne";
-
- if ($ARGV[0] =~ /^-(\w+)$/) {
- $op = $1;
- die "Usage: cpif [ -eq -ne ] file" unless $op =~ /eq|ne/;
- shift;
- }
-
- if ($op eq "ne") {
- $good = 0;
- open(OLD, $ARGV[0]) || goto docopy;
- for (;;) {
- $chunk = sysread(STDIN, $text, 8192);
- $oldlen= sysread(OLD, $old, $chunk);
- if (!$chunk && !$oldlen) {
- close(OLD);
- last;
- } elsif ($oldlen != $chunk || $text ne $old) {
- close(OLD);
- docopy:
- open(NEW, "+>$ARGV[0]") || die "Couldn't open $ARGV[0] for writing";
- seek(NEW, $good, 0);
- syswrite(NEW, $text, $chunk);
- $good += $chunk;
- while($chunk = sysread(STDIN, $text, 8192)) {
- syswrite(NEW, $text, $chunk);
- $good += $chunk;
- }
- truncate(NEW, $good);
- close(NEW);
- last;
- }
- $good += $chunk;
- }
- } else {
- for (;;) {
- $chunk = sysread(STDIN, $text, 8192);
- $oldlen= sysread(OLD, $old, $chunk);
- if (!$chunk && !$oldlen) {
- close(OLD);
- $now = time;
- utime $now, $now, $ARGV[0];
- last;
- } elsif ($oldlen != $chunk || $text ne $old) {
- close(OLD);
- last;
- }
- }
- }